package android.slc.utils;


import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import org.apache.commons.collections4.CollectionUtils;

import java.util.*;

/**
 * @Description:
 * @Author: sunlunchang
 * @CreateDate: 2022/04/06 下午 2:08
 */
public class SlcCollectionUtils {
    /**
     * Adds all elements in the iteration to the given collection.
     *
     * @param collection        the collection to add to, may be null
     * @param elementCollection the elementCollection of elements to add, may be null
     */
    public static <E> void addAll(Collection<E> collection, Collection<E> elementCollection) {
        if (collection == null || elementCollection == null || elementCollection.size() == 0)
            return;
        collection.addAll(elementCollection);
    }

    /**
     * 转换成T2
     *
     * @param collection
     * @param transformer
     * @param <T1>
     * @param <T2>
     * @return
     */
    public static <T1, T2> List<T2> transformT2(Collection<T1> collection, Transformer<T1, T2> transformer) {
        if (collection == null || transformer == null) return new ArrayList<>();
        List<T2> t2List = new ArrayList<>();
        CollectionUtil.forEach(collection, (item, index) -> {
            t2List.add(transformer.transform(item));
        });
        return t2List;
    }

    /**
     * 是否只有一个
     *
     * @return
     */
    public static <T> boolean isOnlyOne(Collection<T> collection) {
        return isOnlyOne(CollectionUtils.size(collection));
    }

    /**
     * 是否只有一个
     * @param size
     * @return
     */
    public static boolean isOnlyOne(int size) {
        return size == 1;
    }

    /**
     * index是第一个
     * @param index
     * @return
     * @param <T>
     */
    public static <T> boolean indexIsFirst(int index) {
        return index == 0;
    }

    /**
     * index是最后一个
     * @param index
     * @param collection
     * @return
     * @param <T>
     */
    public static <T> boolean indexIsLast(int index, Iterable<T> collection) {
        return indexIsLast(index, CollectionUtils.size(collection));
    }

    /**
     * index是最后一个
     * @param index
     * @param size
     * @return
     * @param <T>
     */
    public static <T> boolean indexIsLast(int index, int size) {
        return index == size - 1;
    }

    /**
     * 遍历，为空不accept
     *
     * @param iterable
     * @param consumer
     * @param <T>
     */
    public static <T> void forEachWithNoNull(Iterable<T> iterable, ConsumerNoNull<T> consumer) {
        CollUtil.forEach(iterable, consumer);
    }

    /**
     * 遍历，为空不accept
     *
     * @param iterator
     * @param consumer
     * @param <T>
     */
    public static <T> void forEachWithNoNull(Iterator<T> iterator, ConsumerNoNull<T> consumer) {
        CollUtil.forEach(iterator, consumer);
    }

    /**
     * 遍历，为空不accept
     *
     * @param enumeration
     * @param consumer
     * @param <T>
     */
    public static <T> void forEachWithNoNull(Enumeration<T> enumeration, ConsumerNoNull<T> consumer) {
        CollUtil.forEach(enumeration, consumer);
    }

    /**
     * 过滤空
     *
     * @param <T>
     */
    public interface ConsumerNoNull<T> extends CollUtil.Consumer<T> {
        default void accept(T value, int index) {
            if (value != null) {
                acceptNoNull(value, index);
            }
        }

        void acceptNoNull(T value, int index);
    }

    public interface Transformer<E1, E2> {
        E2 transform(E1 input);
    }
}
